package com.seed.uaa.web.role;

import com.seed.core.annotation.Declares;
import com.seed.core.annotation.EntityClass;
import com.seed.core.annotation.Searches;
import com.seed.core.pojo.R;
import com.seed.core.pojo.SetDTO;
import com.seed.core.pojo.StatPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "角色管理")
@RestController
@RequestMapping("role")
@Declares("id, name")
@Searches("name_like")
@EntityClass(Role.class)
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @Operation(summary = "分页列表")
    @GetMapping("page")
    public R<StatPage<Role>> page(Integer pageNumber, Integer pageSize) {
        return R.data(roleService.page(pageNumber, pageSize));
    }

    @Operation(summary = "保存")
    @PostMapping("save")
    public R<Boolean> save(@RequestBody Role role) {
        return R.data(roleService.save(role));
    }

    @Operation(summary = "更新")
    @GetMapping("update/{id}")
    public R<Role> update(@PathVariable String id) {
        return R.data(roleService.detail(id));
    }

    @Operation(summary = "更新")
    @PutMapping("update")
    public R<Boolean> update(@RequestBody Role role) {
        return R.data(roleService.update(role));
    }

    @Operation(summary = "获取功能权限")
    @GetMapping("actions/{id}")
    public R<String> actions(@PathVariable String id) {
        return R.data(roleService.getActions(id));
    }

    @Operation(summary = "分配功能权限")
    @PutMapping("actions")
    public R<Boolean> actions(@RequestBody SetDTO dto) {
        return R.data(roleService.setActions(dto));
    }

    @Operation(summary = "获取数据权限")
    @GetMapping("datas/{id}")
    public R<List<String>> datas(@PathVariable String id, String actionPath) {
        return R.data(roleService.getDatas(id, actionPath));
    }

    @Operation(summary = "分配数据权限")
    @PutMapping("datas")
    public R<Boolean> datas(String id, String actionPath, @RequestBody List<String> params) {
        return R.data(roleService.setDatas(id, actionPath, params));
    }
    @Operation(summary = "获取字段权限")
    @GetMapping("fields/{id}")
    public R<List<String>> fields(@PathVariable String id, String actionPath) {
        return R.data(roleService.getFields(id, actionPath));
    }

    @Operation(summary = "分配字段权限")
    @PutMapping("fields")
    public R<Boolean> fields(String id, String actionPath, @RequestBody Map<String, String> params) {
        return R.data(roleService.setFields(id, actionPath, params));
    }

    @Operation(summary = "查找角色")
    @GetMapping("lookup")
    public R<StatPage<Role>> lookup(Integer pageNumber, Integer pageSize) {
        return R.data(roleService.page(pageNumber, pageSize));
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public R<Boolean> remove(@PathVariable String id) {
        return R.data(roleService.remove(id));
    }
}
